package com.hackerrank.contests.sep13.challenges.jackgoestorapture;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.TreeSet;

public class Solution {
	private static class Dest implements Comparable<Dest> {
		int n, c;
		public Dest(int n, int c) {
			this.n = n;
			this.c = c;
		}
		@Override
		public int compareTo(Dest e) {
			return c - e.c;
		}
	}
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt(), E = in.nextInt();
		List<Dest> ve[] = new ArrayList[N + 1];
		for(int i = 0; i < E; i++) {
			int n1 = in.nextInt(), n2 = in.nextInt(), c = in.nextInt();
			Dest e1 = new Dest(n2, c);
			if(ve[n1] == null)
				ve[n1] = new ArrayList<Dest>();
			ve[n1].add(e1);
			Dest e2 = new Dest(n1, c);
			if(ve[n2] == null)
				ve[n2] = new ArrayList<Dest>();
			ve[n2].add(e2);
		}
		TreeSet<Dest> t = new TreeSet<Dest>();
		boolean d[] = new boolean[N + 1];
		Dest f = new Dest(1, 0);
		while(f != null && f.n != N) {
			t.remove(f);
			d[f.n] = true;
			for(int i = 0; i < ve[f.n].size(); i++)
				if(!d[ve[f.n].get(i).n])
					t.add(new Dest(ve[f.n].get(i).n, Math.max(f.c, ve[f.n].get(i).c)));

			f = t.isEmpty() ? null : t.first();
		}
		if(f == null)
			System.out.println("NO PATH EXISTS");
		else
			System.out.println(f.c);
	}
}
